package com.mytrip.aop;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;

import com.newheight.annotation.WriteMethod;
import com.newheight.model.BackOperator;
import com.newheight.model.BackendAccessLog;
import com.newheight.service.log.BackendLogManager;
import com.newheight.util.BeanUtil;
import com.newheight.webapp.action.StrutsActionAdapter;
import com.newheight.webapp.filter.CtxSessionBag;

@Aspect
public class BackendAccessLogAspect extends BaseAspect {
	private List<String> actionAccessExcludeList = new ArrayList<String>(0);
	
	private BackendLogManager backendLogManager;
	
	public void setActionAccessExcludeList(List<String> actionAccessExcludeList) {
		this.actionAccessExcludeList = actionAccessExcludeList;
	}

	public void setBackendLogManager(BackendLogManager backendLogManager) {
		this.backendLogManager = backendLogManager;
	}
	
	// ~ method ==================================================================

	@Pointcut("execution(* com.newheight.webapp.action..*Action.*(..)) " +
				"&& !execution(* com.newheight.webapp.action..*Action.get*(..)) " +
				"&& !execution(* com.newheight.webapp.action..*Action.set*(..))")
	public void actionSecurityPt() {
	}
	
	@Around("actionSecurityPt() && target(action)")
	public Object securityAccess(ProceedingJoinPoint pjp, StrutsActionAdapter action) throws Throwable {		
		Method method = ((MethodSignature) pjp.getSignature()).getMethod();	
		String methodName = null;
		String actionName = BeanUtil.extractLShortName(action.getClass());		
		
		for(String aopAccessExcludeActionName :  actionAccessExcludeList){
			if (aopAccessExcludeActionName.equals(actionName)){
				return pjp.proceed();
			}			
		}

		/*if (! method.isAnnotationPresent(WriteMethod.class)){
			return pjp.proceed();
		}*/
		
		BackendAccessLog backendAccessLog = new BackendAccessLog();
		
		backendAccessLog.setActionName(actionName);		
		StringBuffer methodSg = new StringBuffer();
		methodSg.append(method.getName()).append("(");
		
		Class[] argTypes = method.getParameterTypes();
		for(Class argType : argTypes){
			methodSg.append(argTypes.getClass().toString()).append(",");
		}
		if (methodSg.toString().endsWith(",")){
			methodName = methodSg.substring(0, methodSg.length() - 1);
		} else {
			methodName = methodSg.toString();
		}
		
		methodName = methodName + ")";
		backendAccessLog.setMethodName(methodName);
		
		backendAccessLog.setIp(CtxSessionBag.getSessionBag().getIp());
		backendAccessLog.setSessionNbr(CtxSessionBag.getSessionBag().getSessionNbr());		
		BackOperator backOperator = CtxSessionBag.getSessionBag().getBackOperator();
		if (backOperator != null) {
			backendAccessLog.setOperatorId(backOperator.getId());
			backendAccessLog.setOperatorType(BackendAccessLog.OPERATOR_TYPE_BACK_OPERATOR);
		}
		backendAccessLog = backendLogManager.saveBackendAccessLog(backendAccessLog);	

		CtxSessionBag.setBackendAccessLog(backendAccessLog);		
	
		return pjp.proceed();
		
	}
}
